home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / patch20u.lha / patch-2.0.12u9 / inp.c < prev    next >
C/C++ Source or Header  |  1993-05-30  |  10KB  |  364 lines

  1. /* $Header: inp.c,v 2.0.1.1 88/06/03 15:06:13 lwall Locked $
  2.  *
  3.  * $Log:    inp.c,v $
  4.  * Revision 2.0.1.1  88/06/03  15:06:13  lwall
  5.  * patch10: made a little smarter about sccs files
  6.  * 
  7.  * Revision 2.0  86/09/17  15:37:02  lwall
  8.  * Baseline for netwide release.
  9.  * 
  10.  */
  11.  
  12. #include "EXTERN.h"
  13. #include "common.h"
  14. #include "util.h"
  15. #include "pch.h"
  16. #include "INTERN.h"
  17. #include "inp.h"
  18.  
  19. /* Input-file-with-indexable-lines abstract type */
  20.  
  21. static long i_size;            /* size of the input file */
  22. static char *i_womp;            /* plan a buffer for entire file */
  23. static char **i_ptr;            /* pointers to lines in i_womp */
  24.  
  25. static int tifd = -1;            /* plan b virtual string array */
  26. static char *tibuf[2];            /* plan b buffers */
  27. static LINENUM tiline[2] = {-1, -1};    /* 1st line in each buffer */
  28. static LINENUM lines_per_buf;        /* how many lines per buffer */
  29. static int tireclen;            /* length of records in tmp file */
  30.  
  31. /* New patch--prepare to edit another file. */
  32.  
  33. void
  34. re_input()
  35. {
  36.     if (using_plan_a) {
  37.     i_size = 0;
  38. #ifndef lint
  39.     if (i_ptr != Null(char**))
  40.         free((char *)i_ptr);
  41. #endif
  42.     if (i_womp != Nullch)
  43.         free(i_womp);
  44.     i_womp = Nullch;
  45.     i_ptr = Null(char **);
  46.     }
  47.     else {
  48.     using_plan_a = TRUE;        /* maybe the next one is smaller */
  49.     Close(tifd);
  50.     tifd = -1;
  51.     free(tibuf[0]);
  52.     free(tibuf[1]);
  53.     tibuf[0] = tibuf[1] = Nullch;
  54.     tiline[0] = tiline[1] = -1;
  55.     tireclen = 0;
  56.     }
  57. }
  58.  
  59. /* Constuct the line index, somehow or other. */
  60.  
  61. void
  62. scan_input(filename)
  63. char *filename;
  64. {
  65.     if (!plan_a(filename))
  66.     plan_b(filename);
  67.     if (verbose) {
  68.     say3("Patching file %s using Plan %s...\n", filename,
  69.       (using_plan_a ? "A" : "B") );
  70.     }
  71. }
  72.  
  73. /* Try keeping everything in memory. */
  74.  
  75. bool
  76. plan_a(filename)
  77. char *filename;
  78. {
  79.     int ifd, statfailed;
  80.     Reg1 char *s;
  81.     Reg2 LINENUM iline;
  82.     char lbuf[MAXLINELEN];
  83.     int output_elsewhere = strcmp(filename, outname);
  84.  
  85.     statfailed = stat(filename, &filestat);
  86.     if (statfailed && ok_to_create_file) {
  87.     if (verbose)
  88.         say2("(Creating file %s...)\n",filename);
  89.     makedirs(filename, TRUE);
  90.     close(creat(filename, 0666));
  91.     statfailed = stat(filename, &filestat);
  92.     }
  93.     /* For nonexistent or read-only files, look for RCS or SCCS versions.  */
  94.     if (statfailed
  95.     || (! output_elsewhere
  96.         && (/* No one can write to it.  */
  97.         (filestat.st_mode & 0222) == 0
  98.         /* I can't write to it.  */
  99.         || ((filestat.st_mode & 0022) == 0
  100.             && filestat.st_uid != myuid)))) {
  101.     struct stat cstat;
  102.     char *cs = Nullch;
  103.     char *filebase;
  104.     int pathlen;
  105.  
  106.     filebase = basename(filename);
  107.     pathlen = filebase - filename;
  108.  
  109.     /* Put any leading path into `s'.
  110.        Leave room in lbuf for the diff command.  */
  111.     s = lbuf + 20;
  112.     strncpy(s, filename, pathlen);
  113.  
  114. #define try(f, a1, a2) (Sprintf(s + pathlen, f, a1, a2), stat(s, &cstat) == 0)
  115.     if (   try("RCS/%s%s", filebase, RCSSUFFIX)
  116.         || try("RCS/%s"  , filebase,         0)
  117.         || try(    "%s%s", filebase, RCSSUFFIX)) {
  118.         Sprintf(buf, output_elsewhere?CHECKOUT:CHECKOUT_LOCKED, filename);
  119.         Sprintf(lbuf, RCSDIFF, filename);
  120.         cs = "RCS";
  121.     } else if (   try("SCCS/%s%s", SCCSPREFIX, filebase)
  122.            || try(     "%s%s", SCCSPREFIX, filebase)) {
  123.         Sprintf(buf, output_elsewhere?GET:GET_LOCKED, s);
  124.         Sprintf(lbuf, SCCSDIFF, s, filename);
  125.         cs = "SCCS";
  126.     } else if (statfailed)
  127.         fatal2("can't find %s\n", filename);
  128.     /* else we can't write to it but it's not under a version
  129.        control system, so just proceed.  */
  130.     if (cs) {
  131.         if (!statfailed) {
  132.         if ((filestat.st_mode & 0222) != 0)
  133.             /* The owner can write to it.  */
  134.             fatal3("file %s seems to be locked by somebody else under %s\n",
  135.                filename, cs);
  136.         /* It might be checked out unlocked.  See if it's safe to
  137.            check out the default version locked.  */
  138.         if (verbose)
  139.             say3("Comparing file %s to default %s version...\n",
  140.              filename, cs);
  141.         if (system(lbuf))
  142.             fatal3("can't check out file %s: differs from default %s version\n",
  143.                filename, cs);
  144.         }
  145.         if (verbose)
  146.         say3("Checking out file %s from %s...\n", filename, cs);
  147.         if (system(buf) || stat(filename, &filestat))
  148.         fatal3("can't check out file %s from %s\n", filename, cs);
  149.     }
  150.     }
  151.     filemode = filestat.st_mode;
  152.     if (!S_ISREG(filemode))
  153.     fatal2("%s is not a normal file--can't patch\n", filename);
  154.     i_size = filestat.st_size;
  155.     if (out_of_mem) {
  156.     set_hunkmax();        /* make sure dynamic arrays are allocated */
  157.     out_of_mem = FALSE;
  158.     return FALSE;            /* force plan b because plan a bombed */
  159.     }
  160. #ifdef lint
  161.     i_womp = Nullch;
  162. #else
  163.     i_womp = malloc((MEM)(i_size+2));    /* lint says this may alloc less than */
  164.                     /* i_size, but that's okay, I think. */
  165. #endif
  166.     if (i_womp == Nullch)
  167.     return FALSE;
  168.     if ((ifd = open(filename, 0)) < 0)
  169.     pfatal2("can't open file %s", filename);
  170. #ifndef lint
  171.     if (read(ifd, i_womp, (int)i_size) != i_size) {
  172.     Close(ifd);    /* probably means i_size > 15 or 16 bits worth */
  173.     free(i_womp);    /* at this point it doesn't matter if i_womp was */
  174.     return FALSE;    /*   undersized. */
  175.     }
  176. #endif
  177.     Close(ifd);
  178.     if (i_size && i_womp[i_size-1] != '\n')
  179.     i_womp[i_size++] = '\n';
  180.     i_womp[i_size] = '\0';
  181.  
  182.     /* count the lines in the buffer so we know how many pointers we need */
  183.  
  184.     iline = 0;
  185.     for (s=i_womp; *s; s++) {
  186.     if (*s == '\n')
  187.         iline++;
  188.     }
  189. #ifdef lint
  190.     i_ptr = Null(char**);
  191. #else
  192.     i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  193. #endif
  194.     if (i_ptr == Null(char **)) {    /* shucks, it was a near thing */
  195.     free((char *)i_womp);
  196.     return FALSE;
  197.     }
  198.     
  199.     /* now scan the buffer and build pointer array */
  200.  
  201.     iline = 1;
  202.     i_ptr[iline] = i_womp;
  203.     for (s=i_womp; *s; s++) {
  204.     if (*s == '\n')
  205.         i_ptr[++iline] = s+1;    /* these are NOT null terminated */
  206.     }
  207.     input_lines = iline - 1;
  208.  
  209.     /* now check for revision, if any */
  210.  
  211.     if (revision != Nullch) { 
  212.     if (!rev_in_string(i_womp)) {
  213.         if (force) {
  214.         if (verbose)
  215.             say2(
  216. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  217.             revision);
  218.         }
  219.         else if (batch) {
  220.         fatal2(
  221. "this file doesn't appear to be the %s version--aborting.\n", revision);
  222.         }
  223.         else {
  224.         ask2(
  225. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  226.             revision);
  227.         if (*buf != 'y')
  228.         fatal1("aborted\n");
  229.         }
  230.     }
  231.     else if (verbose)
  232.         say2("Good.  This file appears to be the %s version.\n",
  233.         revision);
  234.     }
  235.     return TRUE;            /* plan a will work */
  236. }
  237.  
  238. /* Keep (virtually) nothing in memory. */
  239.  
  240. void
  241. plan_b(filename)
  242. char *filename;
  243. {
  244.     Reg3 FILE *ifp;
  245.     Reg1 int i = 0;
  246.     Reg2 int maxlen = 1;
  247.     Reg4 bool found_revision = (revision == Nullch);
  248.  
  249.     using_plan_a = FALSE;
  250.     if ((ifp = fopen(filename, "r")) == Nullfp)
  251.     pfatal2("can't open file %s", filename);
  252.     if ((tifd = creat(TMPINNAME, 0666)) < 0)
  253.     pfatal2("can't open file %s", TMPINNAME);
  254.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  255.     if (revision != Nullch && !found_revision && rev_in_string(buf))
  256.         found_revision = TRUE;
  257.     if ((i = strlen(buf)) > maxlen)
  258.         maxlen = i;            /* find longest line */
  259.     }
  260.     if (revision != Nullch) {
  261.     if (!found_revision) {
  262.         if (force) {
  263.         if (verbose)
  264.             say2(
  265. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  266.             revision);
  267.         }
  268.         else if (batch) {
  269.         fatal2(
  270. "this file doesn't appear to be the %s version--aborting.\n", revision);
  271.         }
  272.         else {
  273.         ask2(
  274. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  275.             revision);
  276.         if (*buf != 'y')
  277.             fatal1("aborted\n");
  278.         }
  279.     }
  280.     else if (verbose)
  281.         say2("Good.  This file appears to be the %s version.\n",
  282.         revision);
  283.     }
  284.     Fseek(ifp, 0L, 0);        /* rewind file */
  285.     lines_per_buf = BUFFERSIZE / maxlen;
  286.     tireclen = maxlen;
  287.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  288.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  289.     if (tibuf[1] == Nullch)
  290.     fatal1("out of memory\n");
  291.     for (i=1; ; i++) {
  292.     if (! (i % lines_per_buf))    /* new block */
  293.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  294.         pfatal1("can't write temp file");
  295.     if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  296.       == Nullch) {
  297.         input_lines = i - 1;
  298.         if (i % lines_per_buf)
  299.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  300.             pfatal1("can't write temp file");
  301.         break;
  302.     }
  303.     }
  304.     Fclose(ifp);
  305.     Close(tifd);
  306.     if ((tifd = open(TMPINNAME, 0)) < 0) {
  307.     pfatal2("can't reopen file %s", TMPINNAME);
  308.     }
  309. }
  310.  
  311. /* Fetch a line from the input file, \n terminated, not necessarily \0. */
  312.  
  313. char *
  314. ifetch(line,whichbuf)
  315. Reg1 LINENUM line;
  316. int whichbuf;                /* ignored when file in memory */
  317. {
  318.     if (line < 1 || line > input_lines)
  319.     return "";
  320.     if (using_plan_a)
  321.     return i_ptr[line];
  322.     else {
  323.     LINENUM offline = line % lines_per_buf;
  324.     LINENUM baseline = line - offline;
  325.  
  326.     if (tiline[0] == baseline)
  327.         whichbuf = 0;
  328.     else if (tiline[1] == baseline)
  329.         whichbuf = 1;
  330.     else {
  331.         tiline[whichbuf] = baseline;
  332. #ifndef lint        /* complains of long accuracy */
  333.         Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  334. #endif
  335.         if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
  336.         pfatal2("error reading tmp file %s", TMPINNAME);
  337.     }
  338.     return tibuf[whichbuf] + (tireclen*offline);
  339.     }
  340. }
  341.  
  342. /* True if the string argument contains the revision number we want. */
  343.  
  344. bool
  345. rev_in_string(string)
  346. char *string;
  347. {
  348.     Reg1 char *s;
  349.     Reg2 int patlen;
  350.  
  351.     if (revision == Nullch)
  352.     return TRUE;
  353.     patlen = strlen(revision);
  354.     if (strnEQ(string,revision,patlen) && isspace(string[patlen]))
  355.     return TRUE;
  356.     for (s = string; *s; s++) {
  357.     if (isspace(*s) && strnEQ(s+1, revision, patlen) && 
  358.         isspace(s[patlen+1] )) {
  359.         return TRUE;
  360.     }
  361.     }
  362.     return FALSE;
  363. }
  364.